package doantotnghiep.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import doantotnghiep.business.ExportedMedicineBusiness;
import doantotnghiep.business.PurchaseOrderBusiness;
import doantotnghiep.model.ExportedMedicine;
import doantotnghiep.model.PurchaseOrder;
import doantotnghiep.model.PurchaseOrderDetail;
import doantotnghiep.modelDTO.ExportMedicineDTO;
import doantotnghiep.modelDTO.PurchaseOrderDTO;
import doantotnghiep.modelDTO.PurchaseOrderDetailDTO;

@Controller
public class ExportMediacamentController {
	@Autowired
	PurchaseOrderBusiness orderBusiness;
	@Autowired
	ExportedMedicineBusiness exportedMedicineBusiness;
	
	@RequestMapping(value = "/viewAllExported", method = RequestMethod.GET)
	public ModelAndView viewAllExported() {
		Map<String, Object> exportModel = new HashMap<String, Object>();	
		List<ExportedMedicine> exportedList = exportedMedicineBusiness.getAllExport();
			if(exportedList!= null){
				List<ExportMedicineDTO>  exportedMedicineDTOs = new ArrayList<ExportMedicineDTO>();
				for(ExportedMedicine export:exportedList){
					ExportMedicineDTO medicineDTO = new ExportMedicineDTO();
					medicineDTO.setCustomerName(export.getCustomer().getFullName());
					medicineDTO.setDeliveryLocation(export.getDeliveryLocation());
					medicineDTO.setStaffName(export.getStaff().getFullName());
					medicineDTO.setExportededDate(export.getexportededDate());
					medicineDTO.setExportedID(export.getexportedID());
					
					exportedMedicineDTOs.add(medicineDTO);
				}
				exportModel.put("exportedMedicineDTOs", exportedMedicineDTOs);
			}
			return new ModelAndView("viewExport","exportModel",exportModel);
	}

	@RequestMapping(value = "/addExportMedicine", method = RequestMethod.GET)
	public ModelAndView addExportMedicine(){
		return new ModelAndView("viewExportMedicine");
	}
	@RequestMapping(value = "/getAllPurchaseNotExported", method = RequestMethod.GET)
	public ModelAndView viewSelectDate(HttpServletRequest request, ModelMap model,String date) {
		Map<String, Object> exportModel = new HashMap<String, Object>();	
		List<PurchaseOrder> purchaseOrders = orderBusiness.getAllPurchaseNotExported(date);
			if(purchaseOrders!= null){
				List<PurchaseOrderDTO> purchaseOrderDTOs = new ArrayList<PurchaseOrderDTO>();
				for(PurchaseOrder order:purchaseOrders){
					PurchaseOrderDTO purcharseDTO = new PurchaseOrderDTO();
					
					purcharseDTO.setOrderID(order.getOrderID());
					purcharseDTO.setOrderedDate(order.getOrderedDate());
					purcharseDTO.setCustomerName(order.getCustomer().getFullName());
					purcharseDTO.setOrderedDate(order.getOrderedDate());
					purcharseDTO.setDeliveryDate(order.getDeliveryDate());
					purcharseDTO.setDeliveryLocation(order.getDeliveryLocation());
					purcharseDTO.setStaffName(order.getStaff().getFullName());
					purchaseOrderDTOs.add(purcharseDTO);
				}
				exportModel.put("purchaseOrderDTOs", purchaseOrderDTOs);
			}
			return new ModelAndView("viewExportMedicine","exportModel",exportModel);
	}
	
	@RequestMapping(value = "/detailPurchaseOrder", method = RequestMethod.GET)
	public ModelAndView getDetailOrdered(HttpServletRequest request, ModelMap model,int id) {
		
		List<PurchaseOrderDetail> orderDetails = orderBusiness.getAllOrderDetailByID(id);
		if(orderDetails!= null){
			ArrayList<PurchaseOrderDetailDTO> orderDetailDTOs = new ArrayList<PurchaseOrderDetailDTO>();
			for (int i = 0; i < orderDetails.size(); i++) {
				PurchaseOrderDetail pDetail = orderDetails.get(i);
				PurchaseOrderDetailDTO purchaseOrderDetailDTO = new PurchaseOrderDetailDTO();
				purchaseOrderDetailDTO.setId(pDetail.getId());
				purchaseOrderDetailDTO.setMedicament_ID(pDetail.getMedicament()
					.getId());
				purchaseOrderDetailDTO.setMedicamentName(pDetail
						.getMedicament().getMedicamentName());
				purchaseOrderDetailDTO.setTotalBatch(pDetail.getTotalBatch());
				purchaseOrderDetailDTO.setOrderCost(pDetail.getOrderCost());
				orderDetailDTOs.add(purchaseOrderDetailDTO);
			}	
			Map<String, Object> orderDetailModel = new HashMap<String, Object>();
			orderDetailModel.put("orderDetailDTOs", orderDetailDTOs);
			return new ModelAndView("viewOrderDetail", "orderDetailModel",
					orderDetailModel);
		}

		return new ModelAndView();
	}

}
